import {
    request_post
} from '../../utils/request';
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';

Page({
    data: {
        basicCode: null,
        status: null,
        examShow: false,
        choosedType: null,
        tabName: '开始练题'
    },
    onLoad(options) {
        this.setData({
            basicCode: options.code
        })
    },
    onShow() {
        request_post('/exam/home/status', {
            basicCode: this.data.basicCode
        }).then(res => {
            this.setData({
                status: res
            })

            if (res.attentionRead) return

            Dialog.alert({
              width: '80vw',
              title: '请先阅读复习攻略',
            }).then(() => {
              wx.navigateTo({
                url: '/pages/exam-tips/index?type=ATTENTION&code=' + this.data.basicCode + '&confirmed=false'
              })
            });
        })
    },
    toggleExamChoose(event) {
        this.setData({
            choosedType: event.currentTarget.dataset.type,
            examShow: !this.data.examShow
        })
    },
    jumpToTips(event) {
        wx.navigateTo({
            url: '/pages/exam-tips/index?type=' + event.currentTarget.dataset.type + '&code=' + this.data.basicCode + '&confirmed=true'
        })
    },
    onTabChanged(event) {
        this.setData({
            tabName: event.detail.name
        })
    },
    jumpToExamQuestion(event) {
        if (event.currentTarget.dataset.clear == 'false' &&
            ((this.data.choosedType == 'DANXUAN' && this.data.status.danXuanQuestionCount == this.data.status.danXuanAnsweredQuestionCount) ||
                (this.data.choosedType == 'DUOXUAN' && this.data.status.duoXuanQuestionCount == this.data.status.duoXuanAnsweredQuestionCount) ||
                (this.data.choosedType == 'TIANKONG' && this.data.status.tianKongQuestionCount == this.data.status.tianKongAnsweredQuestionCount) ||
                (this.data.choosedType == 'MINGCIJIESHI' && this.data.status.mingCiJieShiQuestionCount == this.data.status.mingCiJieShiAnsweredQuestionCount) ||
                (this.data.choosedType == 'LUNSHU' && this.data.status.lunShuQuestionCount == this.data.status.lunShuAnsweredQuestionCount))
        ) {
            return Toast({
                type: 'html',
                duration: 3000,
                position: 'bottom',
                message: '<div style="text-align:center">已经练完全部题目啦</div><div style="text-align:center">点击<span style="color: #00B5FC;"> 重练本项 </span>再次练习吧</div>'
            });
        }

        this.setData({
            examShow: false
        })
        wx.navigateTo({
            url: '/pages/exam-question/index?fromWrongBook=false&clearPreviousAnswerProcess=' + event.currentTarget.dataset.clear + '&type=' + this.data.choosedType + '&code=' + this.data.basicCode
        })
    },
    jumpToWrongExamQuestion(event) {
        wx.navigateTo({
            url: '/pages/exam-question/index?fromWrongBook=true&type=' + event.currentTarget.dataset.type + '&code=' + this.data.basicCode
        })
    }
})